package com.colorful.insuranceproject.commonActivity;

import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.base.TitleActivity;
import com.colorful.insuranceproject.consts.CodeTable;
import com.colorful.insuranceproject.consts.URLConst;
import com.colorful.insuranceproject.request.UserRequest;
import com.lovely3x.common.managements.user.UserManager;
import com.lovely3x.common.utils.Response;
import com.lovely3x.loginandresgiter.bean.SimpleUser;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by handsome-Bob on 2017/8/16.
 * <p>
 * 核对信息
 */
public class CheckingInformationActivity extends TitleActivity implements TextWatcher {

    private static final int SUBMIT_CHECKING_URL = 0X1;

    private static final int LOAD_BACK_CLICKED = 0X2;

    private UserRequest userRequest;

    @Bind(R.id.tv_name)
    TextView tvName;

    @Bind(R.id.tv_bank)
    TextView tvBank;

    @Bind(R.id.tv_card)
    TextView tvCard;

    @Bind(R.id.et_pay_pwd)
    EditText etPayPwd;

    @Bind(R.id.et_pay_pwd_again)
    EditText etPayPwdAgain;

    @Bind(R.id.img_search_one)
    ImageView imgOne;

    @Bind(R.id.img_search_two)
    ImageView imgTwo;

    @Bind(R.id.tv_agree_and_authorize)
    CheckBox cb;

    @Bind(R.id.tv_set)
    TextView tvSet;

    SimpleUser su;

    private int judgeIconOld;

    private int judgeIconNew;

    @Override
    protected int getContentView() {
        return R.layout.activity_check_info;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        setTitle(getString(R.string.checking_info));
    }

    @Override
    protected void onViewInitialized() {
        judgeIconOld = 0;
        judgeIconNew = 0;
        userRequest = new UserRequest(getHandler());
        etPayPwdAgain.addTextChangedListener(this);

        su = (SimpleUser) UserManager.getInstance().getCurrentUser();
        tvName.setText(su.getUserName());
        tvBank.setText(su.getBankCard());
        tvCard.setText(su.getIdCard());

        tvSet.setClickable(false);

        cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                String pwd1 = etPayPwd.getText().toString().trim();
                String pwd2 = etPayPwdAgain.getText().toString().trim();
                if (isChecked) {
                    if (pwd1.length() != 8 || pwd2.length() != 8) {
                        tvSet.setBackground(ContextCompat.getDrawable(mActivity, R.drawable.btn_gray_such_as_login));
                    } else {
                        tvSet.setBackground(ContextCompat.getDrawable(mActivity, R.drawable.btn_blue_such_as_login));
                        tvSet.setClickable(true);
                    }
                } else {
                    tvSet.setBackground(ContextCompat.getDrawable(mActivity, R.drawable.btn_gray_such_as_login));
                }
            }
        });

    }

    @Override
    protected void handleResponseMessage(Message msg, Response response) {
        super.handleResponseMessage(msg, response);
        switch (msg.what) {
            case SUBMIT_CHECKING_URL:
                if (response.isSuccessful) {
                    showToast(getString(R.string.init_pay_pwd_success));
                    su.setPayPwd(etPayPwd.getText().toString().trim());
                    UserManager.getInstance().notifyUserChanged(su);
                    finish();
                } else {
                    showToast(CodeTable.getInstance().getCodeDescription(response));
                }
                break;

            //  调取退出接口
            case LOAD_BACK_CLICKED:
                if (response.isSuccessful) {
                    launchLoginActivity();
                } else {
                    showToast(CodeTable.getInstance().getCodeDescription(response));
                }
                break;
        }
    }

    @Override
    public void restoreInstanceOnCreateBefore(@NonNull Bundle savedInstance) {

    }

    @Override
    public void restoreInstanceOnCreateAfter(@NonNull Bundle savedInstance) {

    }

    /**
     * 页面监听处理
     */
    @OnClick({R.id.img_search_one, R.id.img_search_two, R.id.tv_checking_encounter_question, R.id.tv_set, R.id.tv_claim_settlement_agreement})
    void OnClicked(View view) {
        switch (view.getId()) {
            //  查看密码
            case R.id.img_search_one:
                if (judgeIconOld == 0) {
                    etPayPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    judgeIconOld = 1;
                    imgOne.setImageResource(R.drawable.icon_bukejian);
                } else {
                    etPayPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    judgeIconOld = 0;
                    imgOne.setImageResource(R.drawable.icon_kejian);
                }
                if (etPayPwd.length() > 1) {
                    etPayPwd.setSelection(etPayPwd.length());
                }
                break;
            //  查看确认密码
            case R.id.img_search_two:
                if (judgeIconNew == 0) {
                    etPayPwdAgain.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    judgeIconNew = 1;
                    imgTwo.setImageResource(R.drawable.icon_bukejian);
                } else {
                    etPayPwdAgain.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    judgeIconNew = 0;
                    imgTwo.setImageResource(R.drawable.icon_kejian);
                }
                if (etPayPwdAgain.length() > 1) {
                    etPayPwdAgain.setSelection(etPayPwdAgain.length());
                }
                break;
            //  核对确认
            case R.id.tv_set:
                String pwd = etPayPwd.getText().toString().trim();
                String payPwd = etPayPwdAgain.getText().toString().trim();
                String name = tvName.getText().toString().trim();
                String bank = tvBank.getText().toString().trim();
                String card = tvCard.getText().toString().trim();
                if (pwd.length() != 8) {
                    showToast(getString(R.string.please_enter_a_pay_password_of_bits_in_length));
                } else if (!pwd.equals(payPwd)) {
                    showToast(getString(R.string.the_two_password_input_is_inconsistent));
                } else if (!cb.isChecked()) {
                    showToast(getString(R.string.please_agree_and_authorize));
                } else {
                    userRequest.initPasswordUrl(pwd, name, bank, card, SUBMIT_CHECKING_URL);
                }
                break;
            //  核对遇到问题
            case R.id.tv_checking_encounter_question:
                String url = URLConst.CHECKING_ENCOUNTER_QUESTION;
                launchActivity(WebViewActivity.class, WebViewActivity.EXTRA_LOAD_URL, url);
                break;
            //  理赔查询服务协议
            case R.id.tv_claim_settlement_agreement:
                String urls = URLConst.CLAIM_AND_SEARCH_SERVER_AGREEMENT;
                launchActivity(WebViewActivity.class, WebViewActivity.EXTRA_LOAD_URL, urls);
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        String pwd1 = etPayPwd.getText().toString().trim();
        String pwd2 = etPayPwdAgain.getText().toString().trim();
        if (s.length() != 8) {
            tvSet.setBackground(ContextCompat.getDrawable(mActivity, R.drawable.btn_gray_such_as_login));
        } else if (s.length() == 8) {
            if (!cb.isChecked()) {
                tvSet.setBackground(ContextCompat.getDrawable(mActivity, R.drawable.btn_gray_such_as_login));
            } else if (pwd1.length() != 8 || pwd2.length() != 8) {
                tvSet.setBackground(ContextCompat.getDrawable(mActivity, R.drawable.btn_gray_such_as_login));
            } else {
                tvSet.setBackground(ContextCompat.getDrawable(mActivity, R.drawable.btn_blue_such_as_login));
                tvSet.setClickable(true);
            }
        }
    }

    @Override
    public void onBackPressed() {
        loadBackClick();
    }

    private void loadBackClick() {
        userRequest.loginOut(LOAD_BACK_CLICKED);
    }

    @Override
    protected void onBackClicked(View v) {
        loadBackClick();
    }
}
